package ca.uwo.android.alumni;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import ca.uwo.android.BaseMenuActivity;
import ca.uwo.android.R;
import ca.uwo.android.rss.RssFeed;
import ca.uwo.android.rss.RssItem;

/**
 * Activity related to the Mustang Tickets page which is a static page showing
 * links to order tickets and get parking info.
 * 
 * @author tracyg
 * 
 */
public class AlumniBranches extends BaseMenuActivity {

	private ListView listView;

	private static final String TAG_NAME = "AlumniBranches";

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alumni_branches);
		String[] branches = getBranchesList();

		listView = (ListView) findViewById(R.id.alumni_branches);
		listView.setClickable(false);
		listView.setAdapter(new ArrayAdapter<String>(this,
				R.layout.alumni_branches_item, branches) {

			// /**
			// * override these two methods to remove the style from the list
			// view
			// * so that the sections are not highlighted when they are selected
			// */
			// public boolean areAllItemsEnabled() {
			// return false;
			// }
			//
			// public boolean isEnabled(int position) {
			// return false;
			// }
		});
	}

	/**
	 * Get a list of branches
	 * 
	 * @return String[] a list of branch names
	 */
	private String[] getBranchesList() {

		String feedLocation = null;

		AlumniBranchesParser parser = null;

		RssFeed feed = null;

		RssItem item = null;

		String title = null;

		ArrayList<String> branchList = new ArrayList<String>();

		try {

			feedLocation = getString(R.string.alumni_branches_link);
//			Log.i(TAG_NAME, "Pulling feed from:  " + feedLocation);

			parser = new AlumniBranchesParser(feedLocation);

			feed = parser.parse();

			if (feed == null) {
				handleError("Unable to get branch names from feed. Feed is null.");
			} else if (feed.size() == 0) {
				handleError("Unable to get branch names from feed. Feed is empty.");
			} else {

//				Log.i(TAG_NAME, "Alumni branches feed found with "
//						+ feed.size() + " items.");

				for (int i = 0; i < feed.size(); i++) {

					item = feed.get(i);

					if (item == null)
						handleError("Unable to get branches from feed.  Item at "
								+ i + " is null.");

					else {
						title = item.getTitle();

						if (title == null)
							handleError("Unable to show branch name. Title of item at "
									+ i + " is null.");

						else {
							branchList.add(title);
						}
					}
				}
			}
		} catch (Exception e) {
			handleException(e, "Failed to pull photo url from feed.");
		}

		// convert branch list to a String[]
		String[] result = new String[branchList.size()];
		for (int i = 0; i < branchList.size(); i++)
			result[i] = branchList.get(i);
		return result;
	}

	/**
	 * This method is called when the "Contact Alumni" button is clicked. It
	 * opens a mail client (or gives the user a choice of mail clients)and fills
	 * in the To field.
	 * 
	 * @param v
	 *            the button view that is clicked
	 */
	public void onContactAlumniButtonClick(View v) {

		String[] recipients = new String[1];
		recipients[0] = getString(R.string.Alumni_Branches_MailTo);

		/* Create the Intent */
		final Intent emailIntent = new Intent(
				android.content.Intent.ACTION_SEND);

		/* Fill it with Data */
		emailIntent.setType("plain/text");
		emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);

		/* Send it off to the Activity-Chooser */
		startActivity(Intent.createChooser(emailIntent,
				"Email Alumni Western..."));

	}

}
